/**
 * Created by zhouli on 18/6/25
 * Email li.zhou@huilianyi.com
 */
import config from 'config';
import httpFetch from 'share/httpFetch';
import errorMessage from 'share/errorMessage';
import {paramsParse} from "share/common";

export default {

  //获取验证码
  getCode(params) {
    // login :验证的手机号 或者邮箱   type ： mobile , email
    // let params = {
    //   login,
    //   type
    // }
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/refactor/account/check/user/token', params)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //校验验证码
  checkCode(params) {
    // let params = {
    //   login,
    //   type,
    //   token
    // }
    //todo
    //已经叫后端马磊改，参数传body
    let queryParams = paramsParse(params);
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/api/refactor/account/check/user' + queryParams)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //导出信息:报表
  exportInfoForReport: function (params, body) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/common/download/by/command?' +
        'exportType=' + params.exportType + "&" +
        'command=' + params.command + "&" +
        'timestamp=' + params.timestamp
        , body)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //导出进度:报表
  exportInfoProgressForReport: function (params) {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/report/api/common/download/async/progress', params)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //导出信息
  exportInfo: function (params, body={}) {
    body= body || {}
    let command =  params.command ? params.command : '';
    return new Promise((resolve, reject) => {
      httpFetch.post(encodeURI(config.baseUrl + '/api/common/download/by/command?' +
        'exportType=' + params.exportType + "&" +
        'command=' + command + "&" +
        'timestamp=' + params.timestamp)
        , body)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //导出进度
  exportInfoProgress: function (params) {
    return new Promise((resolve, reject) => {
      httpFetch.get(encodeURI(config.baseUrl + '/api/common/download/async/progress'), params)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

}

